using System;
using System.Collections.Generic;
using System.Text;

namespace Developex.DatabaseWrapper.Fields
{
	/// <summary>
	/// Fictious field that indicate if field is null or not
	/// </summary>
	public class IndicateField : Field<bool>
	{
		#region Fields

		private IField mainField;

		#endregion

		#region Overrides

		/// <summary>
		/// Expression used in SELECT query
		/// </summary>
		public override string GetSelectExpression()
		{
			return string.Format("(CASE WHEN {0} IS NULL THEN 0 ELSE 1 END)", mainField.GetSelectExpression());
		}

		/// <summary>
		/// Clone
		/// </summary>
		/// <returns></returns>
		public override object Clone()
		{
			IndicateField field = (IndicateField)base.Clone();
			// Note: light-weight clone
			field.mainField = mainField;
			return field;
		}

		///<summary>
		/// Create object 
		///</summary>
		///<returns></returns>
		/// <remarks>
		/// Here should be only object creation, all data copy should be in overrided Clone function.
		/// Used only in base version of Clone function.
		/// Should be overrided for ICloneable interface.
		/// </remarks>
		protected override Field<bool> CreateObjectForClone()
		{
			return new IndicateField(null, null);
		}

		#endregion

		#region Constructors

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="name"></param>
		/// <param name="mainField"></param>
		public IndicateField(string name, IField mainField)
			: base(name, FieldFlags.Fictitious)
		{
			this.mainField = mainField;
		}

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="name"></param>
		/// <param name="flags"></param>
		/// <param name="mainField"></param>
		public IndicateField(string name, FieldFlags flags, IField mainField)
			: base(name, flags | FieldFlags.Fictitious)
		{
			this.mainField = mainField;
		}

		#endregion
	}
}
